package common

import (
	"fmt"
	"gitee.com/shijun123456/kcgin"
	"github.com/gin-gonic/gin"
	"github.com/shenyisyn/goft-gin/goft"
	"os"
	"sinery.net.goft/src/errors"
	"time"
)

// #数据库配置
//db.type=mysql
//db.host=192.168.1.246
//db.port=3306
//db.name=jkd_bus
//db.user=root
//db.pwd=shutongadmin2459
//db.charset=utf8mb4
//db.prefix=kc_
//db.maxidle = 50
//db.maxconn = 100
//db.maxlifetime=12600
func GetMysqlDSN() string {
	// "root:123123@tcp(localhost:3307)/test?charset=utf8mb4&parseTime=True&loc=Local"
	return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=True&loc=Local",
		kcgin.AppConfig.String("db.user"),
		kcgin.AppConfig.String("db.pwd"),
		kcgin.AppConfig.String("db.host"),
		kcgin.AppConfig.String("db.port"),
		kcgin.AppConfig.String("db.name"),
		kcgin.AppConfig.String("db.charset"),
	)
}
func Error(code string, msg string) errors.MyError {
	myerr := errors.MyError{Err: code, ErrMsg: msg, Data: struct{}{}}
	return myerr
}
func Success(data interface{}) goft.Json {
	return gin.H{"err": "0", "errMsg": "", "data": data}
}
func DateFormat() string {
	t := time.Now()
	return t.Format("2006-01-02")
}
func Logger() (*os.File, error) {
	path := kcgin.AppConfig.String("db.log") + DateFormat() + kcgin.AppConfig.String("db.log_prefix")
	f, err := os.Create(path)
	if err != nil {
		return nil, err
	}
	return f, nil
}
